import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useFileStore = defineStore('fileStore', () => {
  // 选中的功能
  const selectedFunction = ref('upload_meter')

  // 通用文件列表
  const fileList = ref([])

  // 水表普查数据提取参数
  const surveyName = ref('')
  const surveyStart = ref(1)
  const surveyEnd = ref(900)

  // 对比功能参数
  const compareKey = ref('ID')

  // 同步备注参数
  const syncKeyColumns = ref('ID')
  const syncRemarkColumn = ref('备注')

  // 按表头拆分参数
  const splitHeaderName = ref('')
  const splitExtraHeader = ref('')
  const splitExtraValues = ref('')

  // 抽查普表清单参数
  const sampleHeaderName = ref('抄表员')
  const sampleFilterValues = ref('')
  const sampleTotalCount = ref(400)

  // 相同数据筛选参数
  const filterHeaderName = ref('用户编号')

  // 数据一致性对比参数
  const consistencyMatchHeader = ref('ID')
  const consistencyCompareHeader = ref('金额')

  // 新增：增强版水表普查参数
  const surveyEnhancedTitleText = ref('水表普查清单')
  const surveyEnhancedStartIndex = ref(0)
  const surveyEnhancedEndIndex = ref(null)
  const surveyEnhancedColumnsToKeep = ref('')

  // 新增：增强版表头细分参数
  const splitEnhancedHeaderName = ref('')
  const splitEnhancedSplitTo = ref('files') // 'files' 或 'sheets'
  const splitEnhancedExtraHeader = ref('')
  const splitEnhancedExtraValues = ref('')

  // 新增：通用数据抽样参数
  const sampleDataHeaderName = ref('')
  const sampleDataFilterValues = ref('')
  const sampleDataTotalCount = ref(400)
  const sampleDataOutputFile = ref('抽样结果.xlsx')
  const sampleDataColumnsToKeep = ref('')

  // 新增：预算计算参数
  const budgetTargetAmount = ref(3700)
  const budgetUsers = ref(JSON.stringify([{
    "name": "何纯荣",
    "address": "新政镇六一二安置区"
  }]))

  // 新增：合并文件参数
  const mergeOutputFilename = ref('合并文件.xlsx')

  // 特殊文件类型
  const meterFiles = ref({
    tpl: null,
    remote: null,
    computers: []
  })

  const compareFiles = ref({
    old: null,
    new: null
  })

  // 计算属性：是否可以处理
  const isProcessable = computed(() => {
    switch (selectedFunction.value) {
      case 'upload_meter':
        return !!meterFiles.value.tpl
      case 'compare':
      case 'sync_remarks':
      case 'filter_common_values':
      case 'compare_header_consistency':
        return !!compareFiles.value.old && !!compareFiles.value.new
      case 'split_by_header':
      case 'sample_meter_list':
      case 'split_by_header_enhanced':
      case 'sample_data':
      case 'budget_calculation':
        return fileList.value.length === 1
      case 'merge_excel_files':
        return fileList.value.length > 0
      default:
        return fileList.value.length > 0
    }
  })

  // 方法
  const updateFileList = (files) => {
    fileList.value = files
  }

  const removeFile = (file) => {
    const index = fileList.value.findIndex(f => f.uid === file.uid)
    if (index > -1) {
      fileList.value.splice(index, 1)
    }
  }

  const updateMeterFile = (type, file) => {
    if (type === 'computers') {
      meterFiles.value.computers.push(file)
    } else {
      meterFiles.value[type] = file
    }
  }

  const removeMeterFile = (type, file) => {
    if (type === 'computers') {
      const index = meterFiles.value.computers.findIndex(f => f.uid === file.uid)
      if (index > -1) {
        meterFiles.value.computers.splice(index, 1)
      }
    } else {
      meterFiles.value[type] = null
    }
  }

  const updateCompareFile = (type, file) => {
    compareFiles.value[type] = file
  }

  const removeCompareFile = (type, file) => {
    compareFiles.value[type] = null
  }

  const clearFiles = () => {
    fileList.value = []
    meterFiles.value = {
      tpl: null,
      remote: null,
      computers: []
    }
    compareFiles.value = {
      old: null,
      new: null
    }
  }

  return {
    // 状态
    selectedFunction,
    fileList,
    surveyName,
    surveyStart,
    surveyEnd,
    compareKey,
    syncKeyColumns,
    syncRemarkColumn,
    splitHeaderName,
    splitExtraHeader,
    splitExtraValues,
    sampleHeaderName,
    sampleFilterValues,
    sampleTotalCount,
    filterHeaderName,
    consistencyMatchHeader,
    consistencyCompareHeader,

    // 新增状态
    surveyEnhancedTitleText,
    surveyEnhancedStartIndex,
    surveyEnhancedEndIndex,
    surveyEnhancedColumnsToKeep,
    splitEnhancedHeaderName,
    splitEnhancedSplitTo,
    splitEnhancedExtraHeader,
    splitEnhancedExtraValues,
    sampleDataHeaderName,
    sampleDataFilterValues,
    sampleDataTotalCount,
    sampleDataOutputFile,
    sampleDataColumnsToKeep,
    budgetTargetAmount,
    budgetUsers,
    mergeOutputFilename,

    meterFiles,
    compareFiles,

    // 计算属性
    isProcessable,

    // 方法
    updateFileList,
    removeFile,
    updateMeterFile,
    removeMeterFile,
    updateCompareFile,
    removeCompareFile,
    clearFiles
  }
})